package com.kedi.usercenter.common;

import java.util.concurrent.TimeUnit;
import com.kedi.usercenter.config.UserConfig;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aliyuncs.exceptions.ClientException;
import com.kedi.usercenter.aliyun.util.PhoneCode;




@RestController
public class SmsController {
	private static final Logger logger = LoggerFactory.getLogger(SmsController.class);

	    @Autowired
	    PhoneCode sendPhone;
	    
	    @Autowired
	    StringRedisTemplate redisTemplate;

	    @Autowired
	    UserConfig  config;
	    
	    @PostMapping(value="/sendMessage")
	    public void sendSms(@RequestParam("phone")String phone) throws ClientException  {
	    	
	    	if(StringUtils.isBlank(phone)){
	            return ;
	        }
	        String vcode =RandomStringUtils.randomNumeric(6);

	        if(StringUtils.isNotBlank(vcode)){
	            sendPhone.sendSms(phone,vcode);
	        }
	        //把验证码保存到redis中
	        redisTemplate.opsForValue().set(config.getkeyPrefix()+phone,vcode,5,TimeUnit.MINUTES);
	    }
}
